package great.easychat.help.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import great.easychat.help.R;
import great.easychat.help.WebUrlActivity;
import great.easychat.help.util.UserInfoManger;


/**
 * 充值提示弹窗
 */
public class RechargeDialog {
    private AlertDialog dialog = null;
    /**
     * @param context  上下文
     * @param callback 回调
     */
    Activity activity;

    public void show(Activity context,boolean isForResult) {
        if (dialog == null) {
            activity = context;
            dialog = new AlertDialog.Builder(context).create();
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCanceledOnTouchOutside(true);// 设置点击屏幕Dialog不消失
            dialog.setCancelable(true);//屏蔽返回按钮
            if (!context.isFinishing()) {
                dialog.show();
            }
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            window.setAttributes(lp);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
            window.setContentView(R.layout.recharge_dialog);
            window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            TextView tvRecharge = window.findViewById(R.id.tvRecharge);
            TextView tvShare = window.findViewById(R.id.tvShare);
            tvRecharge.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isForResult) {
//                        WebUrlActivity.startFor(activity, UserInfoManger.config.getData().getData().getBuy_url(), "激活码购买");
                    } else {
//                        WebUrlActivity.start(activity, UserInfoManger.config.getData().getData().getBuy_url(), "激活码购买");
                    }
                    cancelDialog();
                }
            });

            tvShare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    SharePop sharePop = new SharePop(activity);
                    sharePop.initData("");
//                    sharePop.setShareMsg(UserInfoManger.config.getData().getData().getAbout_content());
                    sharePop.show(activity);
                    cancelDialog();
                }
            });
        }
    }

    /**
     * 取消窗口
     */
    public void cancelDialog() {
        try {
            if (dialog != null) {
                dialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

}
